package com.situ.springboot.controller;

import cn.hutool.extra.servlet.JakartaServletUtil;
import com.situ.springboot.annotation.MyLog;
import com.situ.springboot.pojo.entity.LoginLog;
import com.situ.springboot.pojo.entity.User;
import com.situ.springboot.pojo.query.UserQuery;
import com.situ.springboot.service.ILoginLogService;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@Controller
@RestController  // @RestController=@Controller+@ResponseBody
@RequestMapping("/user")
public class UserController {
    //@Autowired
    //private UserMapper userMapper;

    @Autowired
    private IUserService userService;
    @Autowired
    private ILoginLogService loginLogService;


    //@MyLog
    @MyLog(module = "用户模块", desc = "返回列表")
    @RequestMapping("/list")
    //@ResponseBody
    public PageResult list(UserQuery userQuery) {
        PageResult pageInfo = userService.list(userQuery);
        return pageInfo;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        userService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids) {
       /* String str = null;
        str.substring(1);*/
        userService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(User user) {
        userService.add(user);
        return Result.ok("添加成功");
    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id) {
        User user = userService.selectById(id);
        return Result.ok(user);
    }

    @RequestMapping("/update")
    public Result update(User user) {

        userService.update(user);
        return Result.ok("更新成功");
    }

    @MyLog
    //@RequestMapping(value = "/login", method = RequestMethod.POST)
    @PostMapping(value = "/login")
    public Result login(String name, String password, HttpSession session, HttpServletRequest request) {
        User user = userService.login(name, password);
        if (user == null) {
            LoginLog loginLog = new LoginLog();
            loginLog.setUserName(name);
            loginLog.setStatus(0);
            loginLog.setMsg("用户名或者密码错误");
            loginLog.setIp(JakartaServletUtil.getClientIP(request));
            loginLogService.add(loginLog);
            return Result.error("用户名或者密码错误");
        }

        LoginLog loginLog = new LoginLog();
        loginLog.setUserName(name);
        loginLog.setUserId(user.getId());
        loginLog.setStatus(1);
        loginLog.setMsg("登录成功");
        loginLog.setIp(JakartaServletUtil.getClientIP(request));
        loginLogService.add(loginLog);
        session.setAttribute("user", user);
        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
       session.removeAttribute("user");
       return Result.ok();
    }

    @RequestMapping("/selectAll")
    //@ResponseBody
    public List<User> selectAll() {
        System.out.println("UserController.selectAll");
        /*List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setId(1);
        user1.setName("zhangsan");
        User user2 = new User();
        user2.setId(2);
        user2.setName("list");
        User user3 = new User();
        user3.setId(3);
        user3.setName("wangwu");
        list.add(user1);
        list.add(user2);
        list.add(user3);*/

        List<User> list = userService.selectAll();
        return list;
    }
}
